package org.apache.commons.graph

import kotlinx.atomicfu.locks.SynchronizedObject
import kotlinx.atomicfu.locks.synchronized


/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
 * A synchronized (thread-safe) [Graph] backed by the specified Graph.
 */

internal open class SynchronizedGraph<V, E>(protected val g: Graph<V, E>) : Graph<V, E>,SynchronizedObject() {

    /**
     * {@inheritDoc}
     */
    override fun containsEdge(e: E): Boolean {
        synchronized(this) {
            return g.containsEdge(e)
        }
    }

    /**
     * {@inheritDoc}
     */
    override fun containsVertex(v: V): Boolean {
        synchronized(this) {
            return g.containsVertex(v)
        }
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) {
            return true
        }

        if (other == null || this::class != other::class) {
            return false
        }

        other as SynchronizedGraph<*, *>
        return g == other.g
    }

    /**
     * {@inheritDoc}
     */
    override fun getConnectedVertices(v: V): Iterable<V> {
        synchronized(this) {
            return g.getConnectedVertices(v)
        }
    }

    /**
     * {@inheritDoc}
     */
    override fun getDegree(v: V): Int {
        synchronized(this) {
            return g.getDegree(v)
        }
    }

    /**
     * {@inheritDoc}
     */
    override fun getEdge(source: V, target: V): E? {
        synchronized(this) {
            return g.getEdge(source, target)
        }
    }

    override val edges: Iterable<E>
        /**
         * {@inheritDoc}
         */
        get() {
            synchronized(this) {
                return g.edges
            }
        }

    override val order: Int
        /**
         * {@inheritDoc}
         */
        get() {
            synchronized(this) {
                return g.order
            }
        }

    override val size: Int
        /**
         * {@inheritDoc}
         */
        get() {
            synchronized(this) {
                return g.size
            }
        }

    override val vertices: Iterable<V>
        /**
         * {@inheritDoc}
         */
        get() {
            synchronized(this) {
                return g.vertices
            }
        }

    /**
     * {@inheritDoc}
     */
    override fun getVertices(e: E): VertexPair<V>? {
        synchronized(this) {
            return g.getVertices(e)
        }
    }

    override fun hashCode(): Int {
        val prime = 31
        var result = 1
        result = prime * result + (g.hashCode() ?: 0)
        result = prime * result + (this.hashCode() ?: 0)
        return result
    }

    override fun toString(): String {
        return g.toString()
    }

}

fun<V,E> Graph<V, E>.toSyncGraph(): Graph<V, E> = SynchronizedGraph(this)